package com.onemore.task.model;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.voovan.tools.log.Logger;

import com.alibaba.fastjson.JSONObject;
import com.onemore.task.JdbcGlobal;

public class ModelSpgg2  extends JdbcGlobal{
    final static public String spgg2_field[] = new String[] {"SPDM","GGDM"};
	static public boolean seach_guige2(String ggdm) throws SQLException
    {
    	String sql = "";
        //查询测试
        sql = "select GGDM from GUIGE2 where ggdm='"+ggdm+"'";
        List<Map<String, Object>> listMap = null;
		listMap = getJdbcOperate().queryMapList(sql);
		return listMap.size() > 0;
    }
	/**
     * 插入商品与商品规格2关联表(尺码)    主键SPDM+GGDM
     * @param json
     * @return
     * @throws SQLException 
     */
    static public boolean insert_spgg2(JSONObject json) throws SQLException
    {
    	String fieldStr = "";
    	String valuesStr = "";
    	List<String> listFields = new ArrayList<String>();
    	
    	String guige = json.getString(spgg2_field[1]);
    	if(guige == null) {
    		Logger.error("请求数据错误，规格2数据未填写！");
    		return false;
    	}
    	if(!seach_guige2(guige)) {
    		Logger.error("请求的规格2数据未找到！");
    		return false;
    	}
    	int i = 0;
    	for(i=0;i<spgg2_field.length;i++) {
    		if(json.containsKey(spgg2_field[i]))
    			listFields.add(spgg2_field[i]);
    	}
    	for(i=0;i<listFields.size()-1;i++) {
    		fieldStr += listFields.get(i)+",";
    		valuesStr += "'"+json.getString(listFields.get(i))+"',";
    	}
		fieldStr += listFields.get(i);
		valuesStr += "'"+json.getString(listFields.get(i))+"'";
		//delete("SPGG2",spgg2_field[0]+"='"+json.getString(spgg2_field[0])+"'");
    	String sql = "";
    	if(search("SPGG2", spgg2_field[0]+"='"+json.getString(spgg2_field[0])+"' AND "+spgg2_field[1]+"='"+json.getString(spgg2_field[1])+"'")) {
			//updateDate("SHANGPIN",spgg1_field[0]+"='"+json.getString(spgg1_field[0])+"'",updateStr);
		}else {
	        //查询测试
	        sql = "insert into SPGG2 ("+fieldStr+") values("+valuesStr+")";
	        Map<String, Object> map = null;
			Logger.debug(sql);
			getJdbcOperate().update(sql);
		}
        //查询测试
        /*sql = "insert into SPGG2 ("+fieldStr+") values("+valuesStr+")";
        Map<String, Object> map = null;
		Logger.debug(sql);
		jOperate.update(sql);*/
		return true;
    }
}
